'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 推荐好友信息
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var dataApi = require('../libs/dataApi');
var roleModel = require('../models/roleModel');
var friendModel = require('../models/friendModel');
var unionGroupModel = require('../models/unionGroupModel');
var constantUtil = require('../util/constantUtil');
var onlineService = require('./onlineService');

/**
 * 查询好友信息
 */
exports.getFriendList = function (friend_role_id_list, cb) {
    var friend_list = [];
    async.each(friend_role_id_list, function (role_item, cb) {
        _getFriendInfo(role_item, function (err, result) {
            if (result) {
                friend_list.push(result);
            }
            cb();
        });
    }, function (err) {
        if (!!err) {
            console.error(err);
            return cb();
        }
        cb(null, friend_list);
    });
};

/**
 * 获取推荐好友
 */
exports.getRecommendRoleList = function (role_id, callback) {
    var recommend_role_list = [];
    _getRoleList(role_id, function (err, role_list) {
        _.each(role_list, function (role_item) {
            recommend_role_list.push({
                role_id: role_item.role_id,
                job_type: role_item.job_type,
                name: role_item.name,
                last_login_time: moment(role_item.last_login_time).unix(),
                lv: role_item.lv,
                vip: role_item.vip
            });
        });
        callback(null, recommend_role_list);
    });
};

/**
 * 获取角色列表
 */
var _getRoleList = function (role_id, callback) {
    var role_lv;
    var recommend_role_list = [];
    var friend_all_list = [];
    async.waterfall(
        [
            function (cb) {
                roleModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                role_lv = model.getLv();
                _getRoleListByRoleLevel(role_id, role_lv, cb);
            },
            function (role_list, cb) {
                var friend_role_list = [];
                _.each(role_list, function (role_item) {
                    friend_role_list.push(role_item);
                });
                var role_num = _.size(friend_role_list);
                if (role_num >= 30) {
                    cb(null, friend_role_list);
                } else {
                    var limit_num = 30 - role_num;
                    _getRoleListByLevelSection(role_id, role_lv, limit_num, function (err, role_list) {
                        _.each(role_list, function (role_item) {
                            friend_role_list.push(role_item);
                        });
                        cb(null, friend_role_list);
                    });
                }
            },
            function (role_list, cb) {
                for (var i = 0; i < role_list.length; i++) {
                    friend_all_list.push(role_list[i]);
                }
                cb();
            },

            function (cb) {
                friendModel.getByRoleId(role_id, cb);
            },
            function (model1, cb) {
                var num = constantUtil.value('recommend_friend_num');
                for (var i = 0; i < friend_all_list.length; i++) {
                    if (model1.isFriendRecommendList(friend_all_list[i].role_id)) {
                        if (recommend_role_list.length < num) {
                            recommend_role_list.push(friend_all_list[i]);
                        }
                    }
                }
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
            }
            callback(null, recommend_role_list);
        }
    );
};

var _getRoleListByRoleLevel = function (role_id, role_lv, callback) {
    roleModel.getRoleListByLevel(role_id, role_lv, function (err, role_list) {
        _getRoleListWithoutFriend(role_id, role_list, callback);
    });
};

var _getRoleListByLevelSection = function (role_id, role_lv, limit_num, callback) {
    var min_begin_lv = role_lv - 3;
    var min_end_lv = role_lv - 1;
    var max_begin_lv = role_lv + 1;
    var max_end_lv = role_lv + 3;
    var is_end = false;
    var recommend_role_list = [];
    async.whilst(
        function () {
            var role_lv_count = dataApi.hero_lvl.length();
            return (min_begin_lv >= 1 ||
                min_end_lv >= 1 ||
                max_begin_lv <= role_lv_count ||
                max_end_lv <= role_lv_count) && !is_end;
        },
        function (cb) {
            roleModel.getRoleListByLevelSection(
                min_begin_lv,
                min_end_lv,
                max_begin_lv,
                max_end_lv,
                limit_num,
                function (err, role_list) {
                    _getRoleListWithoutFriend(role_id, role_list, function (err, recommend_list) {
                        _.some(recommend_list, function (role_item) {
                            var role_num = _.size(recommend_role_list);
                            if (role_num >= limit_num) {
                                is_end = true;
                                return;
                            }
                            recommend_role_list.push(role_item);
                        });
                        cb();
                    });
                });
            min_begin_lv -= 3;
            min_end_lv -= 3;
            max_begin_lv += 3;
            max_end_lv += 3;
        },
        function (err) {
            callback(err, recommend_role_list);
        }
    );
};

var _getRoleListWithoutFriend = function (role_id, role_list, callback) {
    var recommend_role_list = [];
    async.waterfall(
        [
            function (cb) {
                friendModel.getByRoleId(role_id, cb);
            },
            function (model, cb) {
                _.each(role_list, function (role_item) {
                    // 判断是否为好友
                    if (!model.getFriend(role_item.role_id)) {
                        recommend_role_list.push(role_item);
                    }
                });
                cb();
            }
        ],
        function (err) {
            callback(err, recommend_role_list);
        }
    );
};

/**
 * 好友信息
 */
var _getFriendInfo = function (friend_role_id, cb) {
    var friend_info = {};
    async.waterfall(
        [
            function (cb) {
                roleModel.getByRoleId(friend_role_id, cb);
             },
            function (role_model, cb) {
                if (!!role_model) {
                    friend_info.name = role_model.name;
                    friend_info.lv = role_model.lv;
                    friend_info.job_type = role_model.job_type;
                    friend_info.role_id = role_model.role_id;
                    friend_info.vip = role_model.vip;
                    friend_info.union_id = role_model.union_id;
                    friend_info.last_login_time = moment(role_model.last_login_time).unix();
                }

                if (role_model.union_id === 0) {
                    friend_info.union_name = '';
                    friend_info.union_flag = 0;
                    cb(null, {});
                } else {
                    unionGroupModel.getById(role_model.union_id, cb);
                }
            },
            function (union_group_model, cb) {
                if (!_.isEmpty(union_group_model)) {
                    friend_info.union_name = union_group_model.name;
                    friend_info.union_flag = union_group_model.union_flag;
                    cb();
                } else {
                    cb();
                }
            },
            function (cb) {
                onlineService.checkRoleOnline(friend_info.role_id, cb);
            },
            function (is_online, cb) {
                friend_info.is_online = +is_online;
                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return cb();
            }
            cb(null, friend_info);
        }
    );
};
